package com.gopay.cashier.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.gopay.common.domain.gopay.GopayAllCustInfo;
import com.gopay.common.util.ContextUtil;
import com.gopay.common.util.PatternCheck;
import com.gopay.remote.scs.dt.DtCheckRemoteService;
import com.gopay.remote.user.UserRemoteService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.gopay.cashier.security.ShiroPluginService;

import java.io.PrintWriter;


/**
 * 单点登录跳转
 * @SSOLoginController.java
 * @author HJY
 * @2013-8-27 上午10:33:09  www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@RequestMapping("/sso")
public class SSOLoginController extends BaseController {

    @Resource(name = "shiroPluginService")
    private ShiroPluginService shiroPluginService;

    @Resource(name = "userRemoteService")
    protected UserRemoteService userRemoteService;


    @Resource(name= "dtCheckRemoteService")
    private DtCheckRemoteService dtCheckRemoteService;
    
    @RequestMapping("/login")
    public ModelAndView preLogin(HttpServletRequest request) {
        printRequest();
        String url = shiroPluginService.getLoginUrl(request);
        return new ModelAndView(new RedirectView(url));
    }


    @RequestMapping("/userInfo")
    @ResponseBody
    public String getUserInfo(String userName){
        JSONObject json = new JSONObject();
        int result = 0;
        GopayAllCustInfo custInfo = null;
        if(StringUtils.isNotBlank(userName)){
            if(PatternCheck.isEmail(userName)){
                custInfo = allCustInfoQueryDAO.findByEmail(userName);
            }else if(PatternCheck.isMobile(userName)){
                custInfo = allCustInfoQueryDAO.findByCustMob(userName);
            }

            if(custInfo != null){
                result = 1;
                json.put("custType",custInfo.getCustType());
                json.put("custStat",custInfo.getCustStat());
            }
        }
        json.put("result",result);
        return json.toString();
    }

    /**
     * 判断用户是否开启动态口令
     * @param userName
     * @return
     */
    @RequestMapping("/isShowDc")
    @ResponseBody
    public String getDc(String userName,String userType){
        JSONObject json = new JSONObject();
        json.put("isOpen", "0");
        String oprId = getRequest().getParameter("oprId");

//      Boolean	openToken = userRemoteService.isOpenDCTokenbyUserId(userName,oprId);

        String custType="10,".contains(userType)?"1":("20,21,22,".contains(userType)?"2":null);
        Boolean openToken = dtCheckRemoteService.isOpenDtLogin(custType, userName, oprId);
        //是否打开动态口令登录
        if(openToken){
            json.put("isOpen", "1");
        }


//        if(userType.equals("20")||userType.equals("21")||userType.equals("21")){
//            Boolean showToken = userRemoteService.isShowToken(userName,userType);
//            if(openToken&&showToken){
//                json.put("isOpen", "1");
//                logger.info("--------cashier 企业用户动态口令登陆------------");
//            }
//        }else if(userType.equals("10")){
//            if(openToken){
//                json.put("isOpen", "1");
//                logger.info("--------cashier 个人用户动态口令登陆------------");
//            }
//        }

        return json.toString();
    }
}
